<?php

// This is a PLUGIN TEMPLATE.

// Copy this file to a new name like abc_myplugin.php.  Edit the code, then
// run this file at the command line to produce a plugin for distribution:
// $ php abc_myplugin.php > abc_myplugin-0.1.txt

// Plugin name is optional.  If unset, it will be extracted from the current
// file name. Uncomment and edit this line to override:
$plugin['name'] = 'wyn_feed_redirector';

$plugin['version'] = '0.1';
$plugin['author'] = 'wayne';
$plugin['author_uri'] = 'http://www.gopherwood.info/';
$plugin['description'] = 'Redirect your feeds';

// Plugin types:
// 0 = regular plugin; loaded on the public web side only
// 1 = admin plugin; loaded on both the public and admin side
// 2 = library; loaded only when include_plugin() or require_plugin() is called
$plugin['type'] = 1; 


@include_once('../zem_tpl.php');

if (0) {
?>
# --- BEGIN PLUGIN HELP ---

<h2>Usage</h2>
<p>Active plugin, and go to extensions - Feed Redirector to set it up</p>
<p>Options:<br />
  <ol>
    <li>Redirect ATOM feed: Handle your atom feed, redirect it to your url</li>
    <li>Redirect RSS feed: Handle your rss feed, redirect it to your url</li>
    <li>Redirect to: The FULL target url you want your feeds be redirect to. It must be the FULL URL, with "http://" or "feed://" or something</li>
    <li>Ignore Feedburner Spider: You should let feedburner get your feed correctly, if you use Feedburner service.</li>
    <li>Ignore Feedsky Spider: You should let feedsky get your feed correctly, if you use Feedsky service.</li>
    <li>*Advanced: Ignore Other Custom Client (Regex to match HTTP USER AGENT): This is for advanced user. If you want to add other user agent which can get the orginal feed, just add it here. What you should know is that the value must be a PHP Regex, for example '/Feedsky/i'</li>
  </ol>
</p>

# --- END PLUGIN HELP ---
<?php
}

# --- BEGIN PLUGIN CODE ---

/*
   Plugin name: wyn_feed_redirector
   Plugin author: wayne
   Plugin version: 0.1
   Author homepage: http://gopherwood.info
   License: GPL
 */

register_callback ('wyn_feed_redirector_handler', 'pretext');

if (@txpinterface == 'admin') {
	$tab = 'Feed Redirector';

	add_privs ("wyn_feed_redirector", '1,2');
	register_tab ("extensions", "wyn_feed_redirector", $tab);
	register_callback ("wyn_feed_redirector_config", "wyn_feed_redirector");
}

function wyn_feed_redirector_config ($event, $step) {
	global $prefs;
	extract ($prefs);

	if (ps ('save')) {
		/* Save */
		pagetop("Feed Redirector", "Preferences Saved");
		$wyn_feed_redirector_atom = ps ('wyn_feed_redirector_atom');
		$wyn_feed_redirector_rss = ps ('wyn_feed_redirector_rss');
		$wyn_feed_redirector_url = ps ('wyn_feed_redirector_url');
		$wyn_feed_redirector_except_feedsky = ps ('wyn_feed_redirector_except_feedsky');
		$wyn_feed_redirector_except_feedburner = ps ('wyn_feed_redirector_except_feedburner');
		$wyn_feed_redirector_except_useragent = ps ('wyn_feed_redirector_except_useragent');
	} else {
		pagetop("Feed Redirector");

		if (!isset ($wyn_feed_redirector_atom)) {
			$wyn_feed_redirector_atom = 1;
		}
		if (!isset ($wyn_feed_redirector_rss)) {
			$wyn_feed_redirector_rss = 1;
		}
		if (!isset ($wyn_feed_redirector_url)) {
			$wyn_feed_redirector_url = "";
		}
		if (!isset ($wyn_feed_redirector_except_feedsky)) {
			$wyn_feed_redirector_except_feedsky = 1;
		}
		if (!isset ($wyn_feed_redirector_except_feedburner)) {
			$wyn_feed_redirector_except_feedburner = 1;
		}
		if (!isset ($wyn_feed_redirector_except_useragent)) {
			$wyn_feed_redirector_except_useragent = "";
		}
	}

	set_pref ("wyn_feed_redirector_atom", $wyn_feed_redirector_atom, "wyn_feed", "2", "yesnoradio");
	set_pref ("wyn_feed_redirector_rss",  $wyn_feed_redirector_rss, "wyn_feed", "2", "yesnoradio");
	set_pref ("wyn_feed_redirector_url",  $wyn_feed_redirector_url, "wyn_feed", "2", "text_input");
	set_pref ("wyn_feed_redirector_except_feedsky", $wyn_feed_redirector_except_feedsky, "wyn_feed", "2", "yesnoradio");
	set_pref ("wyn_feed_redirector_except_feedburner", $wyn_feed_redirector_except_feedburner, "wyn_feed", "2", "yesnoradio");
	set_pref ("wyn_feed_redirector_except_useragent", $wyn_feed_redirector_except_useragent, "wyn_feed", "2", "text_input");

	echo form (
			startTable('edit') .

			tr (
				fLabelCell ("Redirect RSS feed") . 
				td (yesnoRadio ("wyn_feed_redirector_rss", $wyn_feed_redirector_rss))
			   ) .

			tr (
				fLabelCell ("Redirect ATOM feed") . 
				td (yesnoRadio ("wyn_feed_redirector_atom", $wyn_feed_redirector_atom))
			   ) .

			tr (
				fLabelCell ("Redirect to (with protocol name like 'http://')") .
				td (fInput ('text', 'wyn_feed_redirector_url', $wyn_feed_redirector_url))
			   ) .

			tr (
				fLabelCell ("Ignore Feedburner Spider") . 
				td (yesnoRadio ("wyn_feed_redirector_except_feedburner", $wyn_feed_redirector_except_feedburner))
			   ) .

			tr (
					fLabelCell ("Ignore Feedsky Spider") . 
					td (yesnoRadio ("wyn_feed_redirector_except_feedsky", $wyn_feed_redirector_except_feedsky))
			   ) .

			tr (
					fLabelCell ("*Advanced: Ignore Other Custom Client (Regex to match HTTP USER AGENT)") .
					td (fInput ('text', 'wyn_feed_redirector_except_useragent', $wyn_feed_redirector_except_useragent))
			   ) .

			tr(
					td () .
					td (
						fInput ('submit', 'save', gTxt('save'))
					   )
			  ) .

			endTable () .

			eInput ('wyn_feed_redirector') .
			sInput ('save')
			);
}

function wyn_get_request_uri () {
	global $prefs;
	extract ($prefs);

	/* No url set */
	if (!isset ($wyn_feed_redirector_url)) {
		return;
	}

	/* code of this function is from textpattern */
	$request_uri = preg_replace("|^https?://[^/]+|i","",serverSet('REQUEST_URI'));

	// IIS fix
	if (!$request_uri and serverSet('SCRIPT_NAME')) {
		$request_uri = serverSet('SCRIPT_NAME').( (serverSet('QUERY_STRING')) ? '?'.serverSet('QUERY_STRING') : '');
	}

	// another IIS fix
	if (!$request_uri and serverSet('argv')) {
		$argv = serverSet('argv');
		$request_uri = @substr($argv[0], strpos($argv[0], ';') + 1);
	}

	$subpath = preg_quote(preg_replace("/https?:\/\/.*(\/.*)/Ui","$1",hu),"/");
	$req = preg_replace("/^$subpath/i","/",$request_uri);

	return $req;
}

function wyn_feed_redirector_handler ($event, $step) {
	$FEED_BURNER_AGENT = "/FeedBurner/i";
	$FEED_SKY_AGENT = "/FeedSky/i";

	$req = wyn_get_request_uri ();
	extract (chopUrl ($req));

	if (strlen ($u1)) {
		global $prefs;
		extract ($prefs);

		switch ($u1) {
			case 'atom':
				if ($wyn_feed_redirector_atom != 1) {
					return;
				}
				break;

			case 'rss':
				if ($wyn_feed_redirector_rss != 1) {
					return;
				}
				break;

			default:
				return;
		}

		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		if (1 == $wyn_feed_redirector_except_feedburner && preg_match ($FEED_BURNER_AGENT, $user_agent)) {
			return;
		}

		if (1 == $wyn_feed_redirector_except_feedsky && preg_match ($FEED_SKY_AGENT, $user_agent)) {
			return;
		}

		if (trim ($wyn_feed_redirector_except_useragent) != '' && preg_match ($wyn_feed_redirector_except_useragent, $user_agent)) {
			return;
		}

		header("Location:" . trim ($wyn_feed_redirector_url));
		header("HTTP/1.1 307 Temporary Redirect");
		exit ();
	}
}

# --- END PLUGIN CODE ---

?>
